package yelmor.comm100.bean;

import org.igniterealtime.restclient.RestApiClient;
import org.igniterealtime.restclient.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * Created by yel on 4/26/16.
 */
public class OpenfireApi {

  private AuthenticationToken authenticationToken;
  private RestApiClient restApiClient;

  public OpenfireApi(String openfireAdmin, String openfirePasswd, String openfireHost, String openfirePort) {
    authenticationToken = new AuthenticationToken("lS68fUoe8kqomAj9");
    restApiClient = new RestApiClient("http://48.lsu.pw", 9092, authenticationToken);
  }

  public void createOfUser(String userName, String name, String userPasswd) {
    UserEntity userEntity = new UserEntity();
    userEntity.setUsername(userName);
    userEntity.setName(name);
    userEntity.setPassword(userPasswd);
    restApiClient.createUser(userEntity);
  }

  public void createOfGroup(String siteCode) {
    GroupEntity groupEntity = new GroupEntity();
    groupEntity.setName(siteCode);
    restApiClient.createGroup(groupEntity);
  }

  public GroupEntity getServiceGroup(String groupName) {
    return restApiClient.getGroup(groupName);
  }

  public UserEntities getServiceUser(String siteCode) {
    HashMap<String, String> querys = new HashMap<String, String>();
    querys.put("search", "-service-" + siteCode);
    return restApiClient.getUsers(querys);
  }

  public void addUserToGroup(String userName, String groupName) {
    restApiClient.addUserToGroup(userName, groupName);
  }

  public void createService(String serviceName, String servicePasswd, String groupName) {

  }

  public void addCustomRoster(String customJid, String customName, String siteCode) {
    RosterItemEntity rosterItemEntity = new RosterItemEntity(customJid, customName, 3);
    GroupEntity serviceGroup = restApiClient.getGroup(siteCode);
  }

  public RestApiClient getRestApiClient() {
    return restApiClient;
  }
}
